package demo

import (
	"fmt"
	"gitee.com/851537/gmodel/corm"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"os"

	//"github.com/beego/beego/v2/core/logs"
	_ "github.com/go-sql-driver/mysql"
)

func Init(moduleName string) {
	var err error

	logger := NewLogrusLog(moduleName)
	//logger.Logger().Writer()
	hlog.SetLogger(logger)
	logger.SetOutput(os.Stdout)

	//if moduleName == "test" { //debug mode
	//	logger.SetOutput(os.Stdout)
	//}

	dsnFormat := "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local"

	dbHost := "127.0.0.1"
	dbPort := 3306
	dbUser := "root"
	dbPassword := "123456"
	dbName := "kylinx"

	//dbTimezone := global.AppConf.MysqlMain.Timezone
	dsn := fmt.Sprintf(dsnFormat, dbUser, dbPassword, dbHost, dbPort, dbName)
	hlog.Debugf("dsn:%s", dsn)
	client := corm.ClientIns("default")

	//dbLogHost := "127.0.0.1"
	//dbLogPort := 3306
	//dbLogUser := "root"
	//dbLogPassword := "123456"
	//dbLogName := "kylinx_log"
	//logDsn := fmt.Sprintf(dsnFormat, dbLogUser, dbLogPassword, dbLogHost, dbLogPort, dbLogName)
	//logClient := corm.ClientIns("log")

	if !client.IsConnected() {
		client.WithConfig(&corm.Config{
			Driver:      corm.DBDriverMySQL,
			Dsn:         dsn,
			TablePrefix: DbPrefix,
			Logger:      logger.Logger(),
		}).Open()
	}

	//if !logClient.IsConnected() {
	//	logClient.WithConfig(&corm.Config{
	//		Driver:      corm.DBDriverMySQL,
	//		Dsn:         logDsn,
	//		TablePrefix: DbPrefix,
	//		Logger:      logger.Logger(),
	//	}).Open()
	//}

	if true {
		err = client.AutoMigrate(
			new(User),
			new(Printer),
		)
		if err != nil {
			panic(fmt.Sprintf("reg models failed,err:%s", err.Error()))
		}
	}

}

func InitTestEnv(configLayNum ...int) {
	Init("test")
}
